package com.zhouzhou.fastjava.query;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 查询参数
 */
public class QueryArgs extends PageArgs {

    private Map<String, String> staticFilters = Maps.newHashMap();
    /**
     * 查询条件
     */
    private List<QueryFilter> filters = Lists.newArrayList();
    /**
     * 排序
     */
    private List<QuerySortor> sortors = Lists.newArrayList();

    public QueryArgs() {
        super(0, 10);
    }

    public QueryArgs(int start, int limit, Map<String, String> staticFilters, List<QueryFilter> filters, List<QuerySortor> sortors) {
        super(start, limit);
        this.staticFilters = staticFilters;
        this.filters = filters;
        this.sortors = sortors;
    }

    /**
     * 把普通条件 变成static 条件
     * @param names
     * @return
     */
    public QueryArgs doStatic(String... names) {
        for (String item : names) {
            Iterator<QueryFilter> iter = filters.iterator();
            while (iter.hasNext()) {
                QueryFilter filter = iter.next();
                if (filter.getName().equals(item)) {
                    staticFilters.put(item, filter.getValue());
                    iter.remove();
                    break;
                }
            }
        }
        return this;
    }

    /**
     * 根据名称获取过滤条件
     * @param name
     * @return
     */
    public QueryFilter getFilter(String name) {
        for (QueryFilter filter : getFilters()) {
            if (filter.getName().equalsIgnoreCase(name)) {
                return filter;
            }
        }
        return null;
    }

    public Map<String, String> getStaticFilters() {
        return staticFilters;
    }

    public List<QueryFilter> getFilters() {
        return filters;
    }

    public void setFilters(List<QueryFilter> filters) {
        this.filters = filters;
    }

    public List<QuerySortor> getSortors() {
        return sortors;
    }

    public void setSortors(List<QuerySortor> sortors) {
        this.sortors = sortors;
    }

}
